﻿// //   Copyright 2007-2011 Comdiv (F. Sadykov) - http://code.google.com/u/fagim.sadykov/
// //   Supported by Media Technology LTD 
// //    
// //   Licensed under the Apache License, Version 2.0 (the "License");
// //   you may not use this file except in compliance with the License.
// //   You may obtain a copy of the License at
// //    
// //        http://www.apache.org/licenses/LICENSE-2.0
// //    
// //   Unless required by applicable law or agreed to in writing, software
// //   distributed under the License is distributed on an "AS IS" BASIS,
// //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// //   See the License for the specific language governing permissions and
// //   limitations under the License.
// //   
// //   MODIFICATIONS HAVE BEEN MADE TO THIS FILE

using System;
using System.IO;
using Comdiv.QWeb.Serialization;

namespace Comdiv.QWeb.Logging {
	public class TextWriterLogListener : ILogListener {
		private readonly string _format;
		private readonly TextWriter _writer;

		public TextWriterLogListener(TextWriter writer) : this(writer, null, LogLevel.Warning) {
		}


		public TextWriterLogListener(TextWriter writer, LogLevel level) : this(writer, null, level) {
		}

		public TextWriterLogListener(TextWriter writer, string format, LogLevel level) {
			_writer = writer;
			_format = format ?? "{0} {1} {2}";
			Level = level;
		}

		#region ILogListener Members

		public LogLevel Level { get; set; }

		public void debug(object item, QWebContext context = null) {
			if (Level <= LogLevel.Debug) {
				Write("DEBUG", item, context ?? QWebContext.Current);
			}
		}

		public void trace(object item, QWebContext context = null) {
			if (Level <= LogLevel.Trace) {
				Write("TRACE", item, context ?? QWebContext.Current);
			}
		}

		public void info(object item, QWebContext context = null) {
			if (Level <= LogLevel.Info) {
				Write("INFO", item, context ?? QWebContext.Current);
			}
		}

		public void warn(object item, QWebContext context = null) {
			if (Level <= LogLevel.Warning) {
				Write("WARN", item, context ?? QWebContext.Current);
			}
		}

		public void error(object item, QWebContext context = null) {
			if (Level <= LogLevel.Error) {
				Write("ERROR", item, context ?? QWebContext.Current);
			}
		}

		public void fatal(object item, QWebContext context = null) {
			if (Level <= LogLevel.Fatal) {
				Write("FATAL", item, context ?? QWebContext.Current);
			}
		}

		#endregion

		public void Write(string type, object item, QWebContext context = null) {
			if (null == context) {
				_writer.WriteLine(string.Format(_format, DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), type, item));
			}
			else {
				_writer.WriteLine(string.Format(_format, DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), type, item) +
				                 " contextinfo: "
				                 +
				                 new JsSerializer().serialize("Context",
				                                              new {context.ActionName, context.RenderType, context.Parameters})
					);
			}
			_writer.Flush();
		}
	}
}